package com.sugon.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.ModuleMaintainOptionsDao;
import com.sugon.dao.ModuleTypeDao;
import com.sugon.entity.ModuleMaintainOptionsEntity;
import com.sugon.entity.ModuleTypeEntity;
import com.sugon.entity.SysMsgEntity;
import com.sugon.service.ModuleMaintainOptionsHisService;
import com.sugon.service.ModuleMaintainOptionsService;
import com.sugon.service.SysMsgService;
import com.sugon.utils.ProcessInfoEnum;
import com.sugon.utils.ShiroUtils;
import com.sugon.utils.StringUtils;
import com.sugon.utils.SysMsgType;
import com.sugon.utils.excel.ExcelImport;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模具维护规程选项表Service实现类
 *
 * @author sugon
 * @date 2019-08-09 08:58:39
 */
@Service("moduleMaintainOptionsService")
public class ModuleMaintainOptionsServiceImpl implements ModuleMaintainOptionsService {

    @Resource
    private ModuleMaintainOptionsDao moduleMaintainOptionsDao;
    @Resource
    private ModuleMaintainOptionsHisService moduleMaintainOptionsHisService;
    @Resource
    private SysMsgService sysMsgService;
    @Resource
    private ModuleTypeDao moduleTypeDao;

    /**
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public ModuleMaintainOptionsEntity queryObject(String id) {
        return moduleMaintainOptionsDao.queryObject(id);
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<ModuleMaintainOptionsEntity> queryList(Map<String, Object> map) {
        return moduleMaintainOptionsDao.queryList(map);
    }

    /**
     * queryList2
     *
     * @param map
     * @return
     */
    @Override
    public List<ModuleMaintainOptionsEntity> queryList2(Map<String, Object> map) {
        return moduleMaintainOptionsDao.queryList2(map);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return moduleMaintainOptionsDao.queryTotal(map);
    }

    /**
     * queryTotal2
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal2(Map<String, Object> map) {
        return moduleMaintainOptionsDao.queryTotal2(map);
    }

    /**
     * save
     *
     * @param moduleMaintainOptions 实体
     * @return
     */
    @Override
    public int save(ModuleMaintainOptionsEntity moduleMaintainOptions) {
        moduleMaintainOptions.setId(RandomUtil.randomString(32));
        moduleMaintainOptions.setDelFlag(1);
        moduleMaintainOptions.setCreateUser(ShiroUtils.getUserId());
        moduleMaintainOptions.setCreateDate(new Date());
        moduleMaintainOptions.setUpdateDate(moduleMaintainOptions.getCreateDate());
        moduleMaintainOptions.setUpdateUser(moduleMaintainOptions.getCreateUser());
        Map<String, Object> map = new HashMap<>();
        map.put("type", moduleMaintainOptions.getTypeS());
        int orNum = moduleMaintainOptions.getOrNum();
        map.put("orNum", orNum + "");
        //获取当前是否存在序号最大值
        String maxStr = moduleMaintainOptionsDao.loadMaxNum(map);
        int max = StringUtils.isNotEmpty(maxStr) ? Integer.parseInt(maxStr) : 0;
        if (max == 0) {
            moduleMaintainOptions.setOrNum(max);
        } else {
            moduleMaintainOptionsDao.addNum(map);
        }
        return moduleMaintainOptionsDao.save(moduleMaintainOptions);
    }

    /**
     * update
     *
     * @param moduleMaintainOptions 实体
     * @return
     */
    @Override
    public int update(ModuleMaintainOptionsEntity moduleMaintainOptions) {
        moduleMaintainOptions.setUpdateUser(ShiroUtils.getUserId());
        moduleMaintainOptions.setUpdateDate(new Date());

        Map<String, Object> map = new HashMap<>();
        map.put("type", moduleMaintainOptions.getTypeS());
        int orNum = moduleMaintainOptions.getOrNum();
        map.put("orNum", orNum + "");
        /**
         *获取旧数据
         */
        ModuleMaintainOptionsEntity mmoe = moduleMaintainOptionsDao.queryObject(moduleMaintainOptions.getId());
        int oldOrNum = mmoe.getOrNum();
        map.put("oldOrNum", oldOrNum + "");
        //如果调整完成的顺序大于已有数据 则执行减一操作
        if (orNum > oldOrNum) {
            moduleMaintainOptionsDao.editMaxNum(map);
        } else {
            moduleMaintainOptionsDao.editMinNum(map);
        }
        int rows = moduleMaintainOptionsDao.update(moduleMaintainOptions);
        if (rows > 0) {
            SysMsgEntity msgEntity = new SysMsgEntity();
            String Id = moduleMaintainOptions.getId();
            msgEntity.setDataId(Id);
            msgEntity.setDataUrl("../module/modulemaintainoptions.html?Id=" + Id);
            sysMsgService.addApproveItem(ProcessInfoEnum.MDE_MJZL, msgEntity, 0);
        }
        return rows;
    }

    /**
     * delete
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        ModuleMaintainOptionsEntity mmoe = moduleMaintainOptionsDao.queryObject(id);
        Map<String, Object> map = new HashMap<>();
        map.put("type", mmoe.getTypeS());
        int orNum = mmoe.getOrNum();
        map.put("orNum", orNum + "");
        //更新系统排序数据
        moduleMaintainOptionsDao.delNum(map);
        return moduleMaintainOptionsDao.delete(id);
    }

    /**
     * deleteBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return moduleMaintainOptionsDao.deleteBatch(ids);
    }

    /**
     * addNum
     *
     * @param map
     * @return
     */
    @Override
    public int addNum(Map<String, Object> map) {
        return moduleMaintainOptionsDao.addNum(map);
    }

    /**
     * delNum
     *
     * @param map
     * @return
     */
    @Override
    public int delNum(Map<String, Object> map) {
        return moduleMaintainOptionsDao.delNum(map);
    }

    /**
     * loadMaxNum
     *
     * @param map
     * @return
     */
    @Override
    public int loadMaxNum(Map<String, Object> map) {
        //获取当前是否存在序号最大值
        String maxStr = moduleMaintainOptionsDao.loadMaxNum(map);
        int max = StringUtils.isNotEmpty(maxStr) ? Integer.parseInt(maxStr) + 1 : 1;
        return max;
    }

    /**
     * importE
     *
     * @param file
     * @return
     */
    @Override
    public int importE(MultipartFile file) {
        //获取所有表格数据
        List<String[]> list = ExcelImport.getExcelData(file);
        int count = 0;
        if (list != null && list.size() != 0) {
            ModuleMaintainOptionsEntity mmo = new ModuleMaintainOptionsEntity();
            for (int i = 1; i < list.size(); i++) {
                String[] array1 = list.get(i);
                String[] array = new String[10];
                System.arraycopy(array1, 0, array, 0, array1.length);
                //模具类型编码
                if (StringUtils.isNotEmpty(array[0])) {
                    String code = array[0];
                    ModuleTypeEntity mte = moduleTypeDao.queryObjectByCode(code);
                    mmo.setTypeS(mte.getId());
                    mmo.setTypeB(mte.getSuperType());
                }
                //一级名称
                if (StringUtils.isNotEmpty(array[1])) {
                    mmo.setOptionSubclass(array[1]);
                }
                //二级名称
                if (StringUtils.isNotEmpty(array[2])) {
                    mmo.setOptionName(array[2]);
                }
                //序号
                if (StringUtils.isNotEmpty(array[3])) {
                    String no = array[3];
                    mmo.setOrNum(Integer.parseInt(no));
                }
                //方法手段
                if (StringUtils.isNotEmpty(array[4])) {
                    mmo.setMethod(array[4]);
                }
                // 检查标准
                if (StringUtils.isNotEmpty(array[5])) {
                    mmo.setInspectStandard(array[5]);
                }
                // 选项备注
                if (StringUtils.isNotEmpty(array[6])) {
                    mmo.setOptionRemark(array[6]);
                }
                mmo.setId(RandomUtil.randomString(32));
                mmo.setCreateDate(DateUtil.date());
                mmo.setCreateUser(ShiroUtils.getUserId());
                mmo.setUpdateUser(mmo.getCreateUser());
                mmo.setCreateDate(mmo.getCreateDate());

                mmo.setDelFlag(1);
                mmo.setIsEffect(1); // 默认有效
                moduleMaintainOptionsDao.save(mmo);
                count++;
            }
        }
        return count;
    }

    /**
     * check
     *
     * @param map
     * @return
     */
    @Override
    public int check(Map<String, Object> map) {
        String typeStr = map.get("type") + "";
        int type = StringUtils.isNotEmpty(typeStr) ? Integer.parseInt(typeStr) : 0;
        String descn = map.get("descn") + "";
        String id = map.get("id") + "";
        ModuleMaintainOptionsEntity mmo = moduleMaintainOptionsDao.queryObject(id);
        mmo.setIsEffect(type);
        mmo.setDescn(descn);
        mmo.setCheckDate(new Date());
        mmo.setCheckUser(ShiroUtils.getUserId());
        int rows = moduleMaintainOptionsDao.update(mmo);
        if (rows > 0) {
            if (type == 1) {
                moduleMaintainOptionsHisService.recordHistory("DISABLE", mmo);
            }
            ModuleMaintainOptionsEntity moduleMaintainOptions = new ModuleMaintainOptionsEntity();
            SysMsgEntity sysMsgEntity = new SysMsgEntity();
            String Id = moduleMaintainOptions.getId();
            sysMsgEntity.setDataId(Id);
            sysMsgEntity.setDataType(ProcessInfoEnum.MDE_MJZL);
            sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
            sysMsgService.allMsgData(sysMsgEntity);
        }
        return 0;
    }
}
